package cn.openea.perpartner.payment.web.wallet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.Arith;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.payment.web.wallet.dto.SearchDto;
import cn.openea.perpartner.payment.web.wallet.dto.WalletDto;
import cn.openea.perpartner.payment.web.wallet.entity.Bills;
import cn.openea.perpartner.payment.web.wallet.mapper.BillsMapper;
import cn.openea.perpartner.payment.web.wallet.service.IBillsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Weird077
 * @date 2022-03-14 11:38
 */
@Service
public class BillsServiceImpl extends ServiceImpl<BillsMapper, Bills> implements IBillsService {

    @Override
    public List<Bills> selectList(WalletDto dto) {
        LambdaQueryWrapper<Bills> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bills::getUserid, dto.getUserId());
        if (StringUtils.isNotEmpty(dto.getOperType())) {
            wrapper.eq(Bills::getOpertype, dto.getOperType());
        }
        wrapper.orderByDesc(Bills::getCreatetime);
        return list(wrapper);
    }

    @Override
    public boolean insert(WalletDto dto) {
        Bills bills = new Bills();
        BeanUtil.copyProperties(dto, bills,true);
        bills.setCoin(Arith.format(dto.getCoin()));
        return save(bills);
    }

    @Override
    public List<Bills> selectList() {
        LambdaQueryWrapper<Bills> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Bills::getCreatetime);
        return list(wrapper);
    }

    @Override
    public List<Bills> selectListByAccount(SearchDto dto) {
        LambdaQueryWrapper<Bills> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(dto.getUserId())) {
            wrapper.eq(Bills::getUserid, dto.getUserId());
        }
        if (StrUtil.isNotBlank(dto.getOperType())) {
            wrapper.eq(Bills::getOpertype, dto.getOperType());
        }
        if (StrUtil.isNotBlank(dto.getStartTime())) {
            wrapper.ge(Bills::getCreatetime, dto.getStartTime());
        }
        if (StrUtil.isNotBlank(dto.getEndTime())) {
            wrapper.le(Bills::getCreatetime, dto.getEndTime());
        }
        return list(wrapper);
    }

    @Override
    public boolean markBillsRead(SearchDto dto) {
        LambdaUpdateWrapper<Bills> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Bills::getUserid, dto.getUserId());
        wrapper.eq(Bills::getOpertype, dto.getOperType());
        wrapper.eq(Bills::getAlreadyRead, "N");
        wrapper.set(Bills::getAlreadyRead, "Y");
        return update(wrapper);
    }
}
